// pages/styleManagement/add/add.js
import {uploadCOSTxCloud} from '../../../utils/cloud'
import {
  OrderModel
} from '../../../model/order'
var order = new OrderModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    styleName: '',
    styleCode: '',
    goodsPrice: '',
    cycle: '',
    imgs: [],
  },
  onStyleNameChange(e) {
    this.setData({
      styleName:e.detail.value
    })
  },
  onStyleCodeChange(e) {
    this.setData({
      styleCode:e.detail.value
    })
  },
  onGoodsPriceChange(e) {
    this.setData({
      goodsPrice:e.detail.value
    })
  },
  onCycleChange(e) {
    this.setData({
      cycle:e.detail.value
    })
  },
  submit() {
    var picture = this.data.imgs.join(";;")
    order.saveOrUpdate({
      styleName:this.data.styleName,
      styleCode:this.data.styleCode,
      goodsPrice:this.data.goodsPrice,
      cycle:this.data.cycle,
      picture:picture
    }).then(res=>{
      if(res.code==0){
        wx.showToast({
          title: '添加成功',
        })
        wx.navigateBack({
          delta: 0,
        })
      }
      else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseImg() {
    let that = this;
    let len = this.data.imgs;
    if (len >= 1) {
      this.setData({
        lenMore: 1
      })


      return;
    }
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res)
        let tempFilePaths = res.tempFilePaths;
        let imgs = that.data.imgs;
        for (let i = 0; i < tempFilePaths.length; i++) {
          uploadCOSTxCloud('', 'voice', tempFilePaths[i]).then(
            res => {
              if (imgs.length < 1) {
                imgs.push(res)
                that.setData({
                  imgs
                })
              } else {
                that.setData({
                  imgs
                })
                wx.showModal({
                  title: '提示',
                  content: '最多只能有九张图片'
                })
                return;
              }
            }
          )
        }
        that.setData({
          imgs
        })
      }
    })
  },
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs,
    })
  },
  deleteImg(e) {
    let _index = e.currentTarget.dataset.index;
    let imgs = this.data.imgs;
    imgs.splice(_index, 1);
    this.setData({
      imgs
    })
  }
})